﻿/********
* Copyright © 深圳市新元素医疗技术开发有限公司 . All rights reserved.
* 
*  Description:
*  Create By Kain at 5/6/2011 3:24:02 PM
*  
*
*  Revision History:
*  Date                  Who                 What
*  
* 
*/

#region namespace

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

#endregion

namespace Cherry.Agile.Core.Instrumentation
{
    /// <summary>
    /// 
    /// </summary>
    public class DefaultClientInstrumentationProvider : IClientInstrumentationProvider
    {

        const string CATEGORY_NAME = "Remp Client";
        const string NAME = "Remp Client Performance Counter";

        private PerformanceCounter _connectedCounter;
        private PerformanceCounter _disconnetedCounter;
        private PerformanceCounter _faultCounter;
        private PerformanceCounter _activeCounter;

        static DefaultClientInstrumentationProvider()
        {
            SetupCategory();
        }

        /// <summary>
        /// 
        /// </summary>
        public static void SetupCategory()
        {
            if (!PerformanceCounterCategory.Exists(CATEGORY_NAME))
            {

                CounterCreationDataCollection CCDC = new CounterCreationDataCollection();

                CounterCreationData a = new CounterCreationData();
                a.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
                a.CounterName = "Client Created/sec";
                CCDC.Add(a);

                CounterCreationData b = new CounterCreationData();
                b.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
                b.CounterName = "Client Disposed/sec";
                CCDC.Add(b);

                CounterCreationData c = new CounterCreationData();
                c.CounterType = PerformanceCounterType.NumberOfItems32;
                c.CounterName = "Client Actived";
                CCDC.Add(c);


                CounterCreationData d = new CounterCreationData();
                d.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
                d.CounterName = "Client Fault/sec";
                CCDC.Add(d);

                PerformanceCounterCategory.Create(CATEGORY_NAME, NAME, PerformanceCounterCategoryType.SingleInstance, CCDC);

            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static void RemoveCategory()
        {
            if (PerformanceCounterCategory.Exists(CATEGORY_NAME))
            {
                PerformanceCounterCategory.Delete(CATEGORY_NAME);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public DefaultClientInstrumentationProvider()
        {
            InitCounter( );
        }

        private void InitCounter()
        {
            _connectedCounter = new PerformanceCounter(CATEGORY_NAME, "Client Created/sec",  false);

            _disconnetedCounter = new PerformanceCounter(CATEGORY_NAME, "Client Disposed/sec",  false);

            _activeCounter = new PerformanceCounter(CATEGORY_NAME, "Client Actived",false);

            _faultCounter = new PerformanceCounter(CATEGORY_NAME, "Client Fault/sec", false);
        }
        
        /// <summary>
        /// 关闭计数
        /// </summary>
        public void Created()
        {
            _connectedCounter.Increment();
            _activeCounter.Increment();
        }

        /// <summary>
        /// Post Close Connection Event
        /// </summary>
        public void Closed()
        {
            _disconnetedCounter.Increment();
            _activeCounter.Decrement();
        }

        /// <summary>
        /// 失败计数
        /// </summary>
        public void Faulted()
        {
            _faultCounter.Increment();
            _activeCounter.Decrement();
        }

        void IInstrumentation.Setup()
        {
            SetupCategory();
        }

        void IInstrumentation.Remove()
        {
            RemoveCategory();
        }
    }
}
